package com.itfenghuang.day09;



import java.io.*;
import java.nio.Buffer;

public class A00_复习 {
    public static void main(String[] args) {

//        method();
//        method1();
//          method2();

        FileWriter fw=null;
        FileReader fr=null;
        try {
            //字符流  字节流= 字节流 + 编码表
            //创建字符输入流
            fr = new FileReader("D:\\it\\a.txt");
            //创建字符输出流
            fw = new FileWriter("D:\\kk\\b.txt");
//            fw.write("爆爆");
            //遍历输入流文件内容
//            int i;
//            while ((i=fr.read())!=-1){
//                System.out.println((char) i);
//            }
            int len;
           char[] byts= new char[1024];
            while (( len=fr.read(byts) )!=-1){
                fw.write(byts,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fr!=null && fw!=null) {
                try {
                    fr.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    private static void method2() {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            //字节缓冲流
            //字节缓冲输入流  BufferedInputStream
             bis = new BufferedInputStream(new FileInputStream("D:\\it\\b.txt"));
            //字节缓冲输出流  BufferedOutputStream
             bos = new BufferedOutputStream(new FileOutputStream("D:\\kk\\a.txt"));
            int len;
            while ((len=bis.read())!=-1){
                bos.write(len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis!=null && bos!=null){
                try {
                    bis.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void method1() {
        //复制文件及其数据
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            //创建字节输入流
            fis = new FileInputStream("D:\\it\\a.txt");
            //创建字节输出流
            fos = new FileOutputStream("D:\\kk\\b.txt");
            //遍历 字节输入流
//            int i ;
//            while ((i=fis.read())!=-1){
//                System.out.println((char) i);
//            }
//            int len;
//            byte[] byts=new byte[1024];
//            while ((len=fis.read(byts))!=-1){
//                fos.write(byts,0,len);
//            }
//            int i;
//            while ((i=fis.read())!=-1){
//                fos.write(i);
//            }
//            System.out.println(len);


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null && fis!=null){
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    private static void method() {
        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream("D:\\it\\a.txt");
            String str="你有很多饭吃不下";
            fos.write(str.getBytes());
            fos.write("\r\n".getBytes());
//            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //一定会执行,前提是jvm没有退出
            //通常用于一些资源释放及垃圾回收
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
